package bstsetcode.homework;

/**
 * @author noob
 * @version 1.0
 * @date 2021/3/15 10:22
 */
public class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) {
        this.val = val;
    }
    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }




    /**
     * 链表结点的构造函数
     * 创建一个链表，当前ListNode为链表的头结点
     * @param arr
     */
    public ListNode(int[] arr){
        if(arr ==null || arr.length == 0){
            throw new IllegalArgumentException("arr can not be empty.");
        }
        this.val = arr[0];
        ListNode cur = this;
        for (int i = 1; i < arr.length ; i++) {
            cur.next = new ListNode(arr[i]);
            cur = cur.next;
        }
    }



    /*
     * 返回 以当前结点为头结点的链表信息串
     */
    @Override
    public String toString(){
        StringBuilder res  = new StringBuilder();
        ListNode cur = this;
        while (cur != null){
            res.append(cur.val + "->");
            cur = cur.next;
        }
        res.append("NUll");
        return  res.toString();
    }
}
